package cn.sunjiachao.blog4jcs.modules.blog.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.sunjiachao.blog4jcs.modules.blog.service.BlogService;
import cn.sunjiachao.blog4jcs.modules.blog.service.CommentService;
import cn.sunjiachao.blog4jcs_common.model.Blog;
import cn.sunjiachao.blog4jcs_common.model.Comment;
import cn.sunjiachao.blog4jcs_common.util.web.Page;

@Controller
public class BlogController {

	@Resource
	private BlogService blogService;

	@Resource
	private CommentService commentService;

	@RequestMapping(method = RequestMethod.GET, value = "/post")
	public ModelAndView toPostNewBlogPage(Blog blog) {
		ModelAndView mav = new ModelAndView("blog/postNewBlog");
		return mav;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/submit")
	public String submitNewBlog(@ModelAttribute Blog blog) {
		String unsafe = blog.getBlogTitle();
		String safe = Jsoup.clean(unsafe, Whitelist.basic());
		String blogText = Jsoup.clean(blog.getBlogContent(), Whitelist.none());
		blog.setBlogText(blogText.substring(0, 255));
		blog.setBlogTitle(safe);
		blog.setCreateTime(new Date());
		blog.setCreatedUser(1L);
		blog.setBlogType(1);
		blogService.save(blog);
		return "redirect:/";
	}

	@RequestMapping(method = RequestMethod.GET, value = "/queryBlogsAtHomePage")
	public @ResponseBody
	Map<String, Object> queryBlogsAtHomePage(HttpServletRequest request) {
		String currentPage = request.getParameter("p");
		if (currentPage == null) {
			currentPage = "1";
		}
		Page<Blog> page = blogService.queryForPage(Integer.parseInt(currentPage), 7);
		Map<String, Object> resp = new HashMap<String, Object>();
		resp.put("page", page);
		return resp;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/detail/{id}")
	public ModelAndView toBlogDetailPage(@PathVariable String id) {
		Blog blog = blogService.getById(Long.parseLong(id));
		ModelAndView mav = new ModelAndView("blog/blogDetail");
		mav.addObject("blog", blog);
		return mav;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/postComment")
	public @ResponseBody
	Map<String, String> postComment(@ModelAttribute Comment comment) {
		comment.setCreatedTime(new Date());
		commentService.save(comment);
		Map<String, String> resp = new HashMap<>();
		resp.put("result", "发表成功");
		return resp;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/queryDetailPageComments")
	public @ResponseBody
	Map<?, ?> queryDetailPageComments(String blogId) {
		List<Comment> comments = commentService.queryCommentsByBlogId(Long.parseLong(blogId));
		Map<String, Object> map = new HashMap<>();
		map.put("rows", comments);
		return map;
	}

}
